package com.andy.module.util.choose

import android.app.Dialog
import android.widget.TextView
import com.afollestad.materialdialogs.MaterialDialog
import com.andy.module.constant.TimePattern
import com.andy.ui.ext.dateChooser
import com.blankj.utilcode.util.TimeUtils
import com.andy.ui.ext.clickNoRepeat

/**
 *@Author: luofan
 *@CreateDate: 2021/1/27 11:31
 *@Description: 日期选择
 */
class DateChooseManager(val chooseText: TextView, val dialogTitle: String) {

    var chooseTime: Long = 0L

    var chooseListener: ((code: Long) -> Unit)? = null

    private var dialog: Dialog? = null

    init {
        chooseText.clickNoRepeat {
            showDialog()
        }
    }

    private fun showDialog() {
        if (dialog == null) {
            if (chooseTime == 0L) {
                chooseTime = System.currentTimeMillis()
            }
            dialog = MaterialDialog(chooseText.context)
                .title(text = dialogTitle)
                .cancelable(true)
                .cancelOnTouchOutside(false)
                .cornerRadius(12f)
                .dateChooser(currentTime = chooseTime) { dialog, selectTime ->
                    chooseTime = selectTime
                    chooseText.text = TimeUtils.millis2String(chooseTime, TimePattern.YYYY_MM_DD)
                    chooseListener?.invoke(chooseTime)
                }
        }
        dialog?.show()
    }
}
